package com.onefinger.manager.views.model
{
	import com.onefinger.manager.events.ManagerEvent;
	import com.onefinger.manager.vos.Device;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;
	
	public class DeviceCRUDModel extends BaseCRUDModel
	{
		
		//--------------------------------------------------------------------
		//    Public Setters and Getters
		//--------------------------------------------------------------------
		
		
		//.................devices......................
		private var _devices:ArrayCollection;
		
		[Bindable(event="changeDevices")]	
		public function get devices():ArrayCollection
		{
			return _devices;
		}
		public function set devices(value:ArrayCollection):void
		{
			_devices = value;
			dispatchEvent( new Event( "changeDevices" ) );
		}
		
		//.................selectedDevice......................
		private var _selectedDevice:Device;
		
		[Bindable(event="changeDevice")]
		public function get selectedDevice():Device
		{
			return _selectedDevice;
		}
		
		public function set selectedDevice(value:Device):void
		{
			_selectedDevice = value;
			dispatchEvent( new Event( "changeDevice" ) );
		}
		
		//.................tempDevice......................
		 private var _tempDevice:Device=new Device();
		[Bindable(event="changeTempDevice")]		
		public function get tempDevice():Device
		{
			return _tempDevice;
		}
		
		public function set tempDevice(value:Device):void
		{
			_tempDevice = value;
			dispatchEvent(new Event("changeTempDevice"));
		}
		
		//--------------------------------------------------------------------
		//    Constructor
		//--------------------------------------------------------------------
		public function DeviceCRUDModel(dispatcher:IEventDispatcher)
		{
			super(dispatcher);
		}
		//--------------------------------------------------------------------
		//    Public Methods
		//--------------------------------------------------------------------
		
		//.................selectDevice......................
		override public function select(device:Object):void
		{
			super.select(device);
			tempDevice= device.clone();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_DEVICE );
			event.device = device as Device;
			dispatcher.dispatchEvent( event );
		}
		override public function edit(device:Object):void
		{
			super.edit(device);
			select(device);
			stateIndex = BaseCRUDModel.EDITING;
		}
		//.................deleteDevice......................
		override public function remove():void
		{

			super.remove();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.DELETE_DEVICE );
			event.device = selectedDevice;
			dispatcher.dispatchEvent( event );
			unselect();
		}
		
		//.................addNewDevice......................
		override public function addNew():void
		{
			super.addNew();
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.SAVE_DEVICE);
			evet.device = tempDevice;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateDevice......................
		override public function update():void
		{
			super.update();
			selectedDevice.name=tempDevice.name;
			selectedDevice.mac=tempDevice.mac;
			var evet:ManagerEvent = new ManagerEvent(ManagerEvent.UPDATE_DEVICE);
			evet.device = selectedDevice;
			dispatcher.dispatchEvent(evet);
			unselect();
		}
		
		//.................updateDevice......................
		override public function unselect():void
		{
			super.unselect();
			var event:ManagerEvent = new ManagerEvent( ManagerEvent.SELECT_DEVICE );
			event.device = new Device();
			dispatcher.dispatchEvent( event );
			tempDevice = new Device();
		}
		//.................cancel......................
		override public function cancel():void
		{
			super.cancel();
			selectedDevice=new Device();
			tempDevice = new Device();
			stateIndex=BaseCRUDModel.CREATING;
			
		}
		
		//--------------------------------------------------------------------
		//   update on change
		//--------------------------------------------------------------------
		
		public function updateSelectedName(value:String):void
		{
			tempDevice.name=value;
		}
		public function updateSelectedMac(value:String):void
		{
			tempDevice.mac=value;	
		}
		
	}
}